﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atomic.Db;
using Atomic.Permission.Entities;
using Atomic.Common;
using System.Linq.Expressions;
using NHibernate.Linq;
using NHibernate;

/****************************************************************
 * 作者：杜一博
 * 日期：2012/9/18 14:24:49
 * 描述：
 * 
 * --------------------------------------------------------------
 * 修改记录：
 * --------------------------------------------------------------
 * 修改人：
 * 修改日期：
 * 修改内容：
 * 
 ****************************************************************/

namespace Atomic.Permission.DAL
{
    /// <summary>
    /// 描述内容
    /// </summary>
    public class FormDAL : BaseDAL<FormEntity>
    {
        public FormDAL() : base() { }
        public FormDAL(ISession session) : base(session) { }

        /// <summary>
        /// 条件查询系统配置的窗体，按Sort排序
        /// </summary>
        /// <param name="pager">分页参数，获取系统所有窗体，填入null</param>
        /// <param name="predicate">查询条件</param>
        /// <returns>分页的窗体结果</returns>
        public override Pager<FormEntity> GetPager(Pager pager, Expression<Func<FormEntity, bool>> predicate = null)
        {
            Pager<FormEntity> p = null;
            using (session = HibernateHelper.GetSession())
            {
                //按条件确定是否分页查询
                if (pager == null)
                {
                    p = new Pager<FormEntity>();
                    //查询筛选条件
                    if (predicate == null)
                    {
                        p.Records = session.Query<FormEntity>()
                            .OrderBy(m => m.Sort)
                            .ToList();
                    }
                    else
                    {
                        p.Records = session.Query<FormEntity>()
                            .Where(predicate)
                            .OrderBy(m => m.Sort)
                            .ToList();
                    }
                    p.Total = p.Records.Count;
                }
                else
                {
                    //查询筛选条件
                    if (predicate == null)
                    {
                        p = new Pager<FormEntity>
                        {
                            Records = session.Query<FormEntity>()
                                .OrderBy(m => m.Sort)
                                .Skip((pager.Index - 1) * pager.Rows)
                                .Take(pager.Rows)
                                .ToList()
                        };
                        p.Total = session.Query<FormEntity>()
                                .Count();
                    }
                    else
                    {
                        p = new Pager<FormEntity>
                        {
                            Records = session.Query<FormEntity>()
                                .Where(predicate)
                                .OrderBy(m => m.Sort)
                                .Skip((pager.Index - 1) * pager.Rows)
                                .Take(pager.Rows)
                                .ToList()
                        };
                        p.Total = session.Query<FormEntity>()
                                .Where(predicate)
                                .Count();
                    }
                }
            }
            return p;
        }

        /// <summary>
        /// 更新窗体信息，不包括ChildrenCount
        /// </summary>
        /// <param name="entity">窗体信息</param>
        /// <param name="id">要更新窗体的主键</param>
        /// <returns>更新是否成功</returns>
        public override void Update(FormEntity entity, object id)
        {
            FormEntity form = session.Get<FormEntity>(id);
            form.Cls = entity.Cls;
            form.Enabled = entity.Enabled;
            form.Key = entity.Key;
            form.Name = entity.Name;
            form.PID = entity.PID;
            form.Sort = entity.Sort;
            form.Url = entity.Url;
            session.Update(form, id);
            session.Flush();
        }
    }
}
